package com.surfilter.phantomjs.manage;

import com.surfilter.phantomjs.util.HttpUtil;
import org.openqa.selenium.net.PortProber;
import org.openqa.selenium.phantomjs.PhantomJSDriver;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.List;

public class PhantomJSDriverManager implements Comparable<PhantomJSDriverManager>{

    private static final Logger logger = LoggerFactory.getLogger(PhantomJSDriverManager.class);

    private String sessionId;

    private Integer port;

    private PhantomJSDriver driver;

    private boolean use;

    public Integer getPort() {
        return port;
    }

    public void setPort(Integer port) {
        this.port = port;
    }

    private Long useTime;

    private int useNum;

    private List<Long> scores = new ArrayList<Long>();

    protected PhantomJSDriverManager() {
        super();
        this.port = PortProber.findFreePort();
    }

    protected void reopenDriver(){
        closeDriver();
        driver = HttpUtil.getPhantomJSDriver();
        sessionId = driver.getSessionId().toString();
        use = false;
        scores.clear();
    }

    /**
     * reopenDriverByPort
     */
    protected void reopenDriverByPort(){
        closeDriver();
        logger.info("创建phantomjsDriver，通信端口：{}",port);
        driver = HttpUtil.getPhantomJSDriverByPort(port);
        sessionId = driver.getSessionId().toString();
        use = false;
        scores.clear();
    }

    protected void closeDriver(){
        try{
            if(driver != null){
                driver.close();
                driver.quit();
                driver = null;
            }
        }catch(Exception e){
            e.printStackTrace();
            logger.error("关闭PhantomJs异常:", e);
        }
    }

    protected PhantomJSDriverManager(String sessionId, PhantomJSDriver driver, boolean use, Long useTime) {
        super();
        this.sessionId = sessionId;
        this.driver = driver;
        this.use = use;
        this.useTime = useTime;
    }

    public void increasUseNum(){
        useNum ++;
    }

    public String getSessionId() {
        return sessionId;
    }

    public void needReopen(int maxUseNum){
        if(useNum > maxUseNum){
            reopenDriver();
        }
    }

    public void setSessionId(String sessionId) {
        this.sessionId = sessionId;
    }

    public PhantomJSDriver getDriver() {
        return driver;
    }

    public void setDriver(PhantomJSDriver driver) {
        this.driver = driver;
    }

    public boolean isUse() {
        return use;
    }

    public void setUse(boolean use) {
        this.use = use;
    }

    public Long getUseTime() {
        return useTime;
    }

    public void setUseTime(Long useTime) {
        this.useTime = useTime;
    }

    public void score_add(Long score){
        scores.add(score);
    }

    public void score(){
        scores.add(System.currentTimeMillis() - useTime);
    }

    public int sum_score(){
        int result = 0;
        if(scores.size() != 0){
            for(Long score : scores){
                result += score;
            }
            result = result/scores.size();
        }
        return result;
    }

    @Override
    public int compareTo(PhantomJSDriverManager o) {
        // TODO Auto-generated method stub
        if(o.sum_score() > this.sum_score()){
            return -1;
        }else if(o.sum_score() < this.sum_score()){
            return 1;
        }
        return 0;
    }

    public static void main(String[] args) throws InterruptedException {
        PhantomJSDriverManager manager = new PhantomJSDriverManager();
        long start = System.currentTimeMillis();
        manager.reopenDriver();
        long end = System.currentTimeMillis();
        manager.closeDriver();
        System.out.println(end - start);
    }
}
